package com.animatedpicture.viewer;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.animatedpicture.panel.ButtonPanel;
import com.animatedpicture.panel.DrawPanel;

public class MainViewer extends JFrame {

	private static final int FRAME_HEIGHT = 700;
	private static final int FRAME_WIDTH = 900;
	
	private final String title = "Animated Picture"; 
	
	private Container mContainer;
	
	private DrawPanel mDrawPanel = new DrawPanel();
	
	// MenuBar
	private JMenuBar menuBar = new JMenuBar();
	
	// BGM List
	private String[] bgmList = {"Time is running out - Muse", 
								"Don't Stop Me Now - Queen", 
								"Starlight - Muse", 
								"We Will Rock You - Queen", 
								"Uprising - Muse"};
			
	public MainViewer()
	{
		mContainer = this.getContentPane();
		
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
		setTitle(title);
		setResizable(false); // disable resize frame.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		
		ButtonPanel mBtnPanel = new ButtonPanel(this);
		
		// Add Menu - Header
		JMenu mMenu = new JMenu("File");
		JMenu customize = new JMenu("Customize");
		
		// Add Menu - Menu Item
		JMenuItem bgSelect = new JMenuItem("Background");
		JMenuItem bgmSelect = new JMenuItem("Music");
		
		JMenuItem exit = new JMenuItem("Exit");
		
		// Menu Button Action Listener.
		MenuAction mMenuAction = new MenuAction();
		
		bgSelect.addActionListener(mMenuAction);
		bgmSelect.addActionListener(mMenuAction);
		exit.addActionListener(mMenuAction);
		
		mMenu.add(exit);
		
		customize.add(bgSelect);
		customize.add(bgmSelect);
		
		menuBar.add(mMenu);
		menuBar.add(customize);
		
		mContainer.add(menuBar, BorderLayout.NORTH);
		mContainer.add(mBtnPanel, BorderLayout.EAST);
		mContainer.add(mDrawPanel, BorderLayout.CENTER);
		
		setVisible(true);
		
		// Repaint call Timer.
		Timer mTimer = new Timer();
		mTimer.schedule(new timerTask(), new Date(), 90);
	}
	
	class timerTask extends TimerTask
	{
		@Override
		public void run() {
			repaint();
		}
	}

	// Menu Action Listener
	class MenuAction implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			JMenuItem selectedItem = (JMenuItem)e.getSource();
			
			switch (selectedItem.getText()) {
			case "Background":
				System.out.println("Background Selected");
				JFileChooser chooser = new JFileChooser();

				chooser.setMultiSelectionEnabled(true);
				chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				chooser.setFileFilter(new FileNameExtensionFilter("JPG", "jpg"));
				int returnVal = chooser.showOpenDialog(null);
				
				File selectedFile = chooser.getSelectedFile();
				if (selectedFile != null) {
					System.out.println("File name" + selectedFile.getName());
					mDrawPanel.setBackground(selectedFile);
				}
				break;
			case "Music":
				String input = (String)JOptionPane.showInputDialog(null,
						   "Choose Music",             // Message
						   "Choose...",                // Title
						   JOptionPane.QUESTION_MESSAGE,
						   null,
						   bgmList,
						   bgmList[0]);
				mDrawPanel.setBGM(input);
				break;
			case "Exit":
				System.exit(0);
			default:
				break;
			}
			
		}
		
	}
	
}
